//
//  BaziMarrayViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/28.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "BaziMarrayViewController.h"
#import "PGDatePickManager.h"
#import "MarrayInfoViewController.h"

@interface BaziMarrayViewController ()<PGDatePickerDelegate>

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UILabel * topLabel;
@property (nonatomic, weak)UIView * boyView;
@property (nonatomic, weak)UIView * girlView;
@property (nonatomic, weak)UIImageView * boyImageView;
@property (nonatomic, weak)UIImageView * girlImageView;
@property (nonatomic, strong)UILabel * boyNameLabel;
@property (nonatomic, strong)UILabel * boyDateLabel;
@property (nonatomic, strong)UILabel * boyTimeLabel;
@property (nonatomic, strong)UILabel * girlNameLabel;
@property (nonatomic, strong)UILabel * girlDateLabel;
@property (nonatomic, strong)UILabel * girlTimeLabel;
@property (nonatomic, strong)UIButton * boyDateButton;
@property (nonatomic, strong)UIButton * boyTimeButton;
@property (nonatomic, strong)UIButton * girlDateButton;
@property (nonatomic, strong)UIButton * girlTimeButton;
@property (nonatomic, strong)UITextField * boyNameTextFiled;
@property (nonatomic, strong)UITextField * girlNameTextFiled;

@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, copy)NSString * type;

@end

@implementation BaziMarrayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"八字合婚";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    backImageView.image = ImageNamed(@"bj1");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ColorWithRGB(0xCD4B70);
    topView.alpha = 0.8;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    UILabel * topLabel = [UILabel new];
    
    topLabel.text = @"八字合婚\n生辰八字合婚配对测试结果仅供参考，真正的婚姻还是要两个人自己好好经营，祝天下有情人能喜结良缘，终生幸福。";
    topLabel.textColor = WHITE_COLOR;
    topLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    topLabel.numberOfLines = 0;
    topLabel.textAlignment = NSTextAlignmentCenter;
    
    self.topLabel = topLabel;
    
    [self.view addSubview:self.topLabel];
    
    
    UIView * boyView = [UIView new];
    
    boyView.userInteractionEnabled = YES;
    boyView.layer.cornerRadius = 9 * SCREENH6;
    boyView.layer.masksToBounds = YES;
    boyView.backgroundColor = ColorWithRGB(0xFFA8AE);
    
    self.boyView = boyView;
    
    [self.view addSubview:self.boyView];
    
    
    UIImageView * boyImageView = [[UIImageView alloc] init];
    
    boyImageView.image = ImageNamed(@"nan");
    boyImageView.layer.cornerRadius = 25.5 * SCREENH6;
    boyImageView.layer.masksToBounds = YES;
    
    self.boyImageView = boyImageView;
    
    [self.boyView addSubview:self.boyImageView];
    
    
    self.boyNameLabel = [self setupLabelWithText:@"男方姓名:"];
    [self.boyView addSubview:self.boyNameLabel];
    self.boyDateLabel = [self setupLabelWithText:@"出生日期:"];
    [self.boyView addSubview:self.boyDateLabel];
    self.boyTimeLabel = [self setupLabelWithText:@"出生时间:"];
    [self.boyView addSubview:self.boyTimeLabel];
    
    self.boyNameTextFiled = [self setupTextFiled];
    [self.boyView addSubview:self.boyNameTextFiled];
    self.boyDateButton = [self setupButtonWithTitle:@"请选择日期"];
    [self.boyView addSubview:self.boyDateButton];
    self.boyTimeButton = [self setupButtonWithTitle:@"时间不详"];
    [self.boyView addSubview:self.boyTimeButton];
    
    UIView * girlView = [UIView new];
    
    girlView.userInteractionEnabled = YES;
    girlView.layer.cornerRadius = 9 * SCREENH6;
    girlView.layer.masksToBounds = YES;
    girlView.backgroundColor = ColorWithRGB(0xFFA8AE);
    
    self.girlView = girlView;
    
    [self.view addSubview:self.girlView];
    
    
    UIImageView * girlImageView = [[UIImageView alloc] init];
    
    girlImageView.image = ImageNamed(@"nv");
    girlImageView.layer.cornerRadius = 25.5 * SCREENH6;
    girlImageView.layer.masksToBounds = YES;
    
    self.girlImageView = girlImageView;
    
    [self.girlView addSubview:self.girlImageView];
    
    
    self.girlNameLabel = [self setupLabelWithText:@"女方姓名:"];
    [self.girlView addSubview:self.girlNameLabel];
    self.girlDateLabel = [self setupLabelWithText:@"出生日期:"];
    [self.girlView addSubview:self.girlDateLabel];
    self.girlTimeLabel = [self setupLabelWithText:@"出生时间:"];
    [self.girlView addSubview:self.girlTimeLabel];
    
    self.girlNameTextFiled = [self setupTextFiled];
    [self.girlView addSubview:self.girlNameTextFiled];
    self.girlDateButton = [self setupButtonWithTitle:@"请选择日期"];
    [self.girlView addSubview:self.girlDateButton];
    self.girlTimeButton = [self setupButtonWithTitle:@"时间不详"];
    [self.girlView addSubview:self.girlTimeButton];
    
    
    UIButton *sureButton = [UIButton new];
    
    [sureButton setBackgroundImage:ImageNamed(@"lijicesuan") forState:UIControlStateNormal];
    [sureButton setTitle:@"立即测试" forState:UIControlStateNormal];
    [sureButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:19 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(13 * SCREENH6);
        make.height.mas_equalTo(100 * SCREENH6);
        make.width.mas_equalTo(285 * SCREENH6);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topView).offset(12 *SCREENH6);
        make.leading.mas_equalTo(self.topView).offset(12 * SCREENH6);
        make.trailing.mas_equalTo(self.topView).offset(-12 * SCREENH6);
    }];
    
    [self.boyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(30 * SCREENH6);
        make.width.mas_equalTo(265 * SCREENH6);
        make.height.mas_equalTo(140 * SCREENH6);
    }];
    
    [self.boyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyView).offset(6 * SCREENH6);
        make.width.height.mas_equalTo(51 * SCREENH6);
        make.centerY.mas_equalTo(self.boyView);
    }];
    
    [self.boyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.boyView).offset(15 * SCREENH6);
        make.leading.mas_equalTo(self.boyImageView.mas_trailing).offset(9 * SCREENH6);
    }];
    
    [self.boyNameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyView).offset(138 * SCREENH6);
        make.centerY.mas_equalTo(self.boyNameLabel);
        make.trailing.mas_equalTo(self.boyView).offset(-10 * SCREENH6);
        make.height.mas_equalTo(34 * SCREENH6);
    }];
    
    [self.boyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.boyNameLabel.mas_bottom).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.boyNameLabel);
    }];
    
    [self.boyDateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyNameTextFiled);
        make.centerY.mas_equalTo(self.boyDateLabel);
    }];
    
    [self.boyTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.boyDateLabel.mas_bottom).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.boyDateLabel);
    }];
    
    [self.boyTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.boyDateButton);
        make.centerY.mas_equalTo(self.boyTimeLabel);
    }];
    
    [self.girlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.height.width.mas_equalTo(self.boyView);
        make.top.mas_equalTo(self.boyView.mas_bottom).offset(27 * SCREENH6);
    }];
    
    [self.girlImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.leading.mas_equalTo(self.boyImageView);
        make.centerY.mas_equalTo(self.girlView);
    }];
    
    [self.girlNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.girlView).offset(15 * SCREENH6);
        make.leading.mas_equalTo(self.boyTimeLabel);
    }];
    
    [self.girlNameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.boyNameTextFiled);
        make.centerY.mas_equalTo(self.girlNameLabel);
    }];
    
    [self.girlDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.girlNameLabel.mas_bottom).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.girlNameLabel);
    }];
    
    [self.girlDateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.girlNameTextFiled);
        make.centerY.mas_equalTo(self.girlDateLabel);
    }];
    
    [self.girlTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.girlDateLabel.mas_bottom).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.girlDateLabel);
    }];
    
    [self.girlTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.girlDateButton);
        make.centerY.mas_equalTo(self.girlTimeLabel);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.girlView.mas_bottom).offset(30 * SCREENH6);
        make.width.mas_equalTo(182 * SCREENH6);
        make.height.mas_equalTo(43 * SCREENH6);
    }];
}

- (UILabel *)setupLabelWithText:(NSString *)str{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ColorWithRGB(0xFDEB83);
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    label.text = str;
    
    return label;
}

- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickChooseButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    return button;
}


- (UITextField *)setupTextFiled{
    
    UITextField * textFiled = [UITextField new];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor clearColor];
    //设置光标颜色
    textFiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = WHITE_COLOR;//[UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入汉字" attributes:attrs];
    textFiled.textColor = [UIColor whiteColor];
//    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
//    //设置显示模式为永远显示(默认不显示)
//    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    
    return textFiled;
}



- (void)presentDateController {
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGPickerViewType1;
    datePicker.datePickerMode = PGDatePickerModeDateHourMinute;
    datePicker.language = @"zh-Hans";
    [self presentViewController:datePickManager animated:false completion:nil];
    
    //    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    //    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    //    datePicker.minimumDate = [dateFormatter dateFromString: @"2018-02-01 05:04:23"];
    //    datePicker.maximumDate = [dateFormatter dateFromString: @"2028-02-01 05:04:23"];
    
    //    NSDate *date = [dateFormatter dateFromString: @"2020-02-01 05:04:23"];
    //    [datePicker setDate:date animated: true];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    //    self.birthday = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    NSString * date = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)dateComponents.year,(long)dateComponents.month,dateComponents.day];
    NSString * time = [NSString stringWithFormat:@"%ld:%ld",(long)dateComponents.hour,(long)dateComponents.minute];
    
    if ([self.type isEqualToString:@"one"]) {
        
        [self.boyDateButton setTitle:date forState:UIControlStateNormal];
        [self.boyTimeButton setTitle:time forState:UIControlStateNormal];
    }else if ([self.type isEqualToString:@"two"]){
        
        [self.girlDateButton setTitle:date forState:UIControlStateNormal];
        [self.girlTimeButton setTitle:time forState:UIControlStateNormal];
    }

}



- (void)clickSureButton{
    
    if (self.boyNameTextFiled.text.length >0 && self.girlNameTextFiled.text.length > 0) {
        
        [[SuanNetWorkTool sharedTools] POST:@"/index/Liuhedivine/colligatePair" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary * data = responseObject[@"data"];
            
            HIDDEN_TAB;
            
            MarrayInfoViewController * infoVC = [MarrayInfoViewController new];
            
            infoVC.colligate_score = data[@"colligate_score"];
            infoVC.colligate_content = data[@"colligate_content"];
            
            [self.navigationController pushViewController:infoVC animated:YES];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请先完善所需信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}


- (void)clickChooseButton:(UIButton *)button{
    
    if (button == self.boyTimeButton || button == self.boyDateButton) {
        
        self.type = @"one";
    }else if (button == self.girlDateButton || button == self.girlTimeButton){
        
        self.type = @"two";
    }
    
    [self presentDateController];
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
